package com.example.magichealth;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class ModifyProfile extends Activity implements android.view.View.OnClickListener {
	
	private static String mnom = null;//appelÈ par la fonction intent.putExtra l.37
	private static String MNMON = "mnom";//appelÈ par la fonction intent.putExtra l.37

//--------------------------------------------------------------------------------------------------------------------------\\	
/**
*	Initialisation of the activity
* @param view
*/

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_modify_profile);
	
    // Show the Up button in the action bar.
    setupActionBar();
    
	Intent intent = getIntent();
	intent.putExtra(MNMON, mnom); 
	
	//appel fonction modifier
	setVariables();
	//creation of the button "modify"
	Button b1 = (Button)findViewById(R.id.mmodifier);
    b1.setOnClickListener(this);
    
    
    
	
}

//--------------------------------------------------------------------------------------------------------------------------\\	
																										   //***Navigation***\\				
/**
* To download the 
* @param view
*/

	public void goToProfile(View view) {
		Intent intent = new Intent(this, Profil.class);
		startActivity(intent);
	}
	//--------------------------------------------------------------------------------------------------------------------------\\	
																									  //****Gestion of Button****\\
/**
* To attribute the action on click
*  @param view
*/


	 public void onClick(View v){
	    	
	    	switch(v.getId()){
	    	
	    	case R.id.mmodifier:
	    		
	    		
	    		//*******LASTNAME*****\\
	    		EditText text1 = (EditText)findViewById(R.id.mnom);
	    		String mnom = text1.getText().toString();
				String name1 = "nom_file";
				if(!mnom.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name1, Context.MODE_PRIVATE);
					fos.write(mnom.getBytes());
					fos.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				}
				//-----------------------------------\\

				
	    		
				//******FIRSTNAME*****\\
	    		EditText text2 = (EditText)findViewById(R.id.mprenom);
	    		String mprenom = text2.getText().toString();
				String name2 = "prenom_file";
				if(!mprenom.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name2, Context.MODE_PRIVATE);
					fos.write(mprenom.getBytes());
					fos.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	    		
				}
	    		
				//-----------------------------------\\
				
	    		
	    		//*****SEXE*****\\
				
	    		RadioButton sexm = (RadioButton)findViewById(R.id.mmasc);
	    		RadioButton sexf = (RadioButton)findViewById(R.id.mfem);
	    		if(sexm.isChecked()||sexf.isChecked())
	    		{
	    		try {
	    			String sexe = null;
		    		if(sexm.isChecked())
		    			{
		    				sexe="Masculin";
		    			}
		    		if(sexf.isChecked())
		    			{
		    				sexe="Feminin";
		    			}
		    		String name3 = "sexe_file";
					FileOutputStream fos = openFileOutput(name3, Context.MODE_PRIVATE);
					fos.write(sexe.getBytes());
					fos.close();
					} 
	    			catch (FileNotFoundException e) {
	    			// TODO Auto-generated catch block
					e.printStackTrace();
	    			} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
	    			}
	    		}
	    		
	    		
	    	//-----------------------------------\\
    		
	    	
    		//*****DAY OF BIRTH*****\\
	    		EditText text4 = (EditText)findViewById(R.id.mjour);
	    		String mjour = text4.getText().toString();
				String name4 = "jour_file";
				if(!mjour.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name4, Context.MODE_PRIVATE);
					fos.write(mjour.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
	    		
				}
	    		
				//-----------------------------------\\
	    		
				
	    		//******MONTH OF BIRTH*****\\

	    		EditText text5 = (EditText)findViewById(R.id.mmois);
	    		String mmois = text5.getText().toString();
				String name5 = "mois_file";
				if(!mmois.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name5, Context.MODE_PRIVATE);
					fos.write(mmois.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
	    		
				//-----------------------------------\\
	    		
	    		
	    		//******YEAR OF BIRTH******\\
	    		EditText text6 = (EditText)findViewById(R.id.mannee);
	    		String mannee = text6.getText().toString();
				String name6 = "annee_file";
				if(!mannee.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name6, Context.MODE_PRIVATE);
					fos.write(mannee.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
	    		
				//******PLACE OF BIRTH*******\\

	    		EditText text7 = (EditText)findViewById(R.id.mlieuNaissance);
	    		String mlieu = text7.getText().toString();
				String name7 = "lieu_file";
				if(!mlieu.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name7, Context.MODE_PRIVATE);
					fos.write(mlieu.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
	    		
				}
	    		
				//-----------------------------------\\
	    		
				
	    		//******WEIGHT*****\\

	    		EditText text8 = (EditText)findViewById(R.id.mpoids);
	    		String mpoids = text8.getText().toString();
				String name8 = "poids_file";
				if(!mpoids.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name8, Context.MODE_PRIVATE);
					fos.write(mpoids.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
	    		
				//-----------------------------------\\
	    		
	    		
	    		//******SIZEMetre*****\\

	    		EditText text91 = (EditText)findViewById(R.id.mtaillemetre);
	    		String mtaillemetre = text91.getText().toString();
				String name91 = "taillem_file";
				if(!mtaillemetre.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name91, Context.MODE_PRIVATE);
					fos.write(mtaillemetre.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
				//******SIZEcm*****\\

	    		EditText text92 = (EditText)findViewById(R.id.mtaillecm);
	    		String mtaillecm = text92.getText().toString();
				String name92 = "taillecm_file";
				if(!mtaillecm.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name92, Context.MODE_PRIVATE);
					fos.write(mtaillecm.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
						}
				}
				//-----------------------------------\\
	    		
	    		
	    		//********NAME OF PERSON TRUSTED 1*******\\
	    		EditText text10 = (EditText)findViewById(R.id.mnomurgence1);
	    		String mnomurgence1 = text10.getText().toString();
				String name10 = "nomurgence1_file";
				if(!mnomurgence1.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name10, Context.MODE_PRIVATE);
					fos.write(mnomurgence1.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
				
				//-----------------------------------\\
				
				
				////********NUMBER OF PERSON TRUSTED 1*******\\
				EditText text11 = (EditText)findViewById(R.id.mtelephoneUrgence1);
	    		String mtelephoneUrgence1 = text11.getText().toString();
				String name11 = "telurgence1_file";
				if(!mtelephoneUrgence1.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name11, Context.MODE_PRIVATE);
					fos.write(mtelephoneUrgence1.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
				//-----------------------------------\\
				
	    		
				//********NAME OF PERSON TRUSTED 2*******\\
	    		EditText text12 = (EditText)findViewById(R.id.mnomurgence2);
	    		String mnomurgence2 = text12.getText().toString();
				String name12 = "nomurgence2_file";
				if(!mnomurgence2.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name12, Context.MODE_PRIVATE);
					fos.write(mnomurgence2.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
				
				//-----------------------------------\\
				
				
				//********NUMBER OF PERSON TRUSTED 2*******\\

				EditText text13 = (EditText)findViewById(R.id.mtelephoneUrgence2);
	    		String mtelephoneUrgence2 = text13.getText().toString();
				String name13 = "telurgence2_file";
				if(!mtelephoneUrgence2.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name13, Context.MODE_PRIVATE);
					fos.write(mtelephoneUrgence2.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
				
				//-----------------------------------\\
				
				
				//********NAME DOCTOR*******\\
	    		EditText text14 = (EditText)findViewById(R.id.mnommedecin);
	    		String mnommedecin = text14.getText().toString();
				String name14 = "nommedecin_file";
				if(!mnommedecin.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name14, Context.MODE_PRIVATE);
					fos.write(mnommedecin.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
	    		
				//-----------------------------------\\
				
	    		
				//********NUMBER DOCTOR*******\\
				EditText text15 = (EditText)findViewById(R.id.mtelephonemedecin);
	    		String mtelephonemedecin = text15.getText().toString();
				String name15 = "telmedecin_file";
				if(!mtelephonemedecin.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name15, Context.MODE_PRIVATE);
					fos.write(mtelephonemedecin.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
				
				//-----------------------------------\\
				
	    		
				//********MORE INFORMATIONS*******\\
				EditText text16 = (EditText)findViewById(R.id.minfos);
	    		String minfos = text16.getText().toString();
				String name16 = "infos_file";
				if(!minfos.matches(""))
				{
					try {
					FileOutputStream fos = openFileOutput(name16, Context.MODE_PRIVATE);
					fos.write(minfos.getBytes());
					fos.close();
					} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					}
				}
			
				
	 goToProfile(v);
	 break;
	 }
}
	 
				
				
	 
	//--------------------------------------------------------------------------------------------------------------------------\\	

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.modify_profile, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
		
			Intent intent = new Intent(this, Profil.class); // retourner au profil
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	
	public void setVariables()
	{
		
		//*******SET FIRSTNAME IN EDITTEXT*****\\
		EditText text1 = (EditText)findViewById(R.id.mprenom);
		
		try {
			
			FileInputStream fis = openFileInput("prenom_file");
		
				byte[] b1 = new byte[20];
				fis.read(b1);
				fis.close();
			
			String setprenom = new String(b1);
		    text1.setText(setprenom);	

			} 
			catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			}	
	
		
		//*******SET NAME IN EDITTEXT*****\\
			EditText text2 = (EditText)findViewById(R.id.mnom);
				
			try {
					
					FileInputStream fis = openFileInput("nom_file");
				
						byte[] b1 = new byte[20];
						fis.read(b1);
						fis.close();
					
					String setnom = new String(b1);
				    text2.setText(setnom);	

				} 
			catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
		
				
				//*******SET SEX IN EDITTEXT*****\\
			
			RadioButton sexm = (RadioButton)findViewById(R.id.mmasc);
	    	RadioButton sexf = (RadioButton)findViewById(R.id.mfem);

			try {
								
					FileInputStream fis = openFileInput("sexe_file");
							
					byte[] b1 = new byte[20];
					fis.read(b1);
					fis.close();					
									
					String setsexm = new String(b1);
					String setsexf = new String(b1);
				
	    		
					if(setsexm.compareTo("Masculin")==0)
					{
			    	sexm.setChecked(true);		
					}
					if(setsexf.compareTo("Feminin")==0)
					{
			    	sexf.setChecked(true);
					}					


				} 
				catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}				
							
					
			//*******SET DATE OF BIRTH IN EDITTEXT*****\\
			
			//*Jour*\\
			EditText text4 = (EditText)findViewById(R.id.mjour);
			
			try {
				
				FileInputStream fis = openFileInput("jour_file");
			
					byte[] b1 = new byte[2];
					fis.read(b1);
					fis.close();
				
				String setdatej = new String(b1);
			    text4.setText(setdatej);	

			} 
			catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*Mois*\\
			
			EditText text5 = (EditText)findViewById(R.id.mmois);
			
			try {
				
				FileInputStream fis = openFileInput("mois_file");
			
					byte[] b1 = new byte[2];
					fis.read(b1);
					fis.close();
				
				String setdatem = new String(b1);
			    text5.setText(setdatem);	

			}
			catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*Année*\\

			EditText text6 = (EditText)findViewById(R.id.mannee);
			
			try {
				
				FileInputStream fis = openFileInput("annee_file");
			
					byte[] b1 = new byte[4];
					fis.read(b1);
					fis.close();
				
				String setdatea = new String(b1);
			    text6.setText(setdatea);	

			} 
			catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}		
					
			//*******SET PLACE OF BIRTH IN EDITTEXT*****\\
			EditText text7 = (EditText)findViewById(R.id.mlieuNaissance);
			
			try {
				
				FileInputStream fis = openFileInput("lieu_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String setlieu = new String(b1);
			    text7.setText(setlieu);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
				
			//*******SET WEIGHT IN EDITTEXT*****\\
			EditText text8 = (EditText)findViewById(R.id.mpoids);
			
			try {
				
				FileInputStream fis = openFileInput("poids_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String setpoids = new String(b1);
			    text8.setText(setpoids);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}	
				
			//*******SET SIZE IN EDITTEXT*****\\
			
			//taille (metre)//
			EditText text91 = (EditText)findViewById(R.id.mtaillemetre);
			
			try {
				
				FileInputStream fis = openFileInput("taillem_file");
			
					byte[] b1 = new byte[2];
					fis.read(b1);
					fis.close();
				
				String settaillem = new String(b1);
			    text91.setText(settaillem);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//taille (centimetre)//
			EditText text92 = (EditText)findViewById(R.id.mtaillecm);
			
			try {
				
				FileInputStream fis = openFileInput("taillecm_file");
			
					byte[] b1 = new byte[2];
					fis.read(b1);
					fis.close();
				
				String settaillecm = new String(b1);
			    text92.setText(settaillecm);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*******SET NAME EMERGENCY 1 IN EDITTEXT*****\\
			EditText text10 = (EditText)findViewById(R.id.mnomurgence1);
			
			try {
				
				FileInputStream fis = openFileInput("nomurgence1_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String seturgence1 = new String(b1);
			    text10.setText(seturgence1);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}

			
			//*******SET TEL EMERGENCY 1 IN EDITTEXT*****\\
			EditText text11 = (EditText)findViewById(R.id.mtelephoneUrgence1);
			
			try {
				
				FileInputStream fis = openFileInput("telurgence1_file");
			
					byte[] b1 = new byte[15];
					fis.read(b1);
					fis.close();
				
				String settel1 = new String(b1);
			    text11.setText(settel1);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*******SET NAME EMERGENCY 2 IN EDITTEXT*****\\
			EditText text12 = (EditText)findViewById(R.id.mnomurgence2);
			
			try {
				
				FileInputStream fis = openFileInput("nomurgence2_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String seturgence2 = new String(b1);
			    text12.setText(seturgence2);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*******SET TEL EMERGENCY 2 IN EDITTEXT*****\\
			EditText text13 = (EditText)findViewById(R.id.mtelephoneUrgence2);
			
			try {
				
				FileInputStream fis = openFileInput("telurgence2_file");
			
					byte[] b1 = new byte[15];
					fis.read(b1);
					fis.close();
				
				String settel2 = new String(b1);
			    text13.setText(settel2);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*******SET NAME MEDECIN IN EDITTEXT*****\\
			EditText text14 = (EditText)findViewById(R.id.mnommedecin);
			
			try {
				
				FileInputStream fis = openFileInput("nommedecin_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String setmedecin = new String(b1);
			    text14.setText(setmedecin);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			//*******SET TEL MEDECIN IN EDITTEXT*****\\
			EditText text15 = (EditText)findViewById(R.id.mtelephonemedecin);
			
			try {
				
				FileInputStream fis = openFileInput("telmedecin_file");
			
					byte[] b1 = new byte[30];
					fis.read(b1);
					fis.close();
				
				String settelmedecin = new String(b1);
			    text15.setText(settelmedecin);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
			
			
			//*******SET INFOS IN EDITTEXT*****\\
			EditText text16 = (EditText)findViewById(R.id.minfos);
			
			try {
				
				FileInputStream fis = openFileInput("infos_file");
			
					byte[] b1 = new byte[300];
					fis.read(b1);
					fis.close();
				
				String setinfo = new String(b1);
			    text16.setText(setinfo);	

			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
						
	}
	

	
}
